package handler

import (
	"sep22-hw/model"

	"github.com/gin-gonic/gin"
)

func RoleNode(c *gin.Context) {
	var node []model.Node

	//select * from nodes left join role_nodes on role_nodes.nid = nodes.id

	model.DB.Table("nodes").
		Joins("left join role_nodes on role_nodes.nid = nodes.id").
		Find(&node).
		Debug()

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": node,
	})
	return
}

func RoleNodeSelect(c *gin.Context) {
	id := c.Query("id")

	var roleNode model.RoleNode
	err := model.DB.Where("id=?", id).Find(&roleNode).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": roleNode,
	})
	return
}
